package com.facebook.drawee.drawable;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.VisibleForTesting;
import java.util.Arrays;

public final class FadeDrawable extends ArrayDrawable {
    @VisibleForTesting public int mTransitionState;
    @VisibleForTesting public boolean[] mIsLayerOn;
    private final Drawable[] e;
    @VisibleForTesting private int f;
    @VisibleForTesting private long g;
    @VisibleForTesting private int[] h;
    @VisibleForTesting private int[] i;
    @VisibleForTesting private int j;
    @VisibleForTesting private int k;

    public FadeDrawable(Drawable[] arg6) {
        super(arg6);
        int v4 = 255;
        boolean v0 = arg6.length > 0 ? true : false;
        Preconditions.checkState(v0, "At least one layer required!");
        this.e = arg6;
        this.h = new int[arg6.length];
        this.i = new int[arg6.length];
        this.j = v4;
        this.mIsLayerOn = new boolean[arg6.length];
        this.k = 0;
        this.mTransitionState = 2;
        Arrays.fill(this.h, 0);
        this.h[0] = v4;
        Arrays.fill(this.i, 0);
        this.i[0] = v4;
        Arrays.fill(this.mIsLayerOn, false);
        this.mIsLayerOn[0] = true;
    }

    private boolean a(float arg9) {
        int v7 = 255;
        int v0 = 0;
        boolean v2 = true;
        while(v0 < this.e.length) {
            int v4 = this.mIsLayerOn[v0] ? 1 : -1;
            this.i[v0] = ((int)((((float)(v4 * 255))) * arg9 + (((float)this.h[v0]))));
            if(this.i[v0] < 0) {
                this.i[v0] = 0;
            }

            if(this.i[v0] > v7) {
                this.i[v0] = v7;
            }

            if((this.mIsLayerOn[v0]) && this.i[v0] < v7) {
                v2 = false;
            }

            if(!this.mIsLayerOn[v0] && this.i[v0] > 0) {
                v2 = false;
            }

            ++v0;
        }

        return v2;
    }

    public final void beginBatchMode() {
        ++this.k;
    }

    public final void endBatchMode() {
        --this.k;
        this.invalidateSelf();
    }

    public final void setTransitionDuration(int arg3) {
        this.f = arg3;
        if(this.mTransitionState == 1) {
            this.mTransitionState = 0;
        }
    }

    public final void finishTransitionImmediately() {
        this.mTransitionState = 2;
        int v0;
        for(v0 = 0; v0 < this.e.length; ++v0) {
            int[] v3 = this.i;
            int v2 = this.mIsLayerOn[v0] ? 255 : 0;
            v3[v0] = v2;
        }

        this.invalidateSelf();
    }

    public final void draw(Canvas arg9) {
        int v1 = 2;
        int v3 = 0;
        boolean v2 = true;
        switch(this.mTransitionState) {
            case 0: {
                System.arraycopy(this.i, 0, this.h, 0, this.e.length);
                this.g = SystemClock.uptimeMillis();
                float v0 = this.f == 0 ? 1f : 0f;
                boolean v4 = this.a(v0);
                int v0_1 = v4 ? v1 : 1;
                this.mTransitionState = v0_1;
                v2 = v4;
                break;
            }
            case 1: {
                boolean v0_2 = this.f > 0 ? true : false;
                Preconditions.checkState(v0_2);
                v0_2 = this.a((((float)(SystemClock.uptimeMillis() - this.g))) / (((float)this.f)));
                if(!v0_2) {
                    v1 = 1;
                }

                this.mTransitionState = v1;
                v2 = v0_2;
                break;
            }
        }

        while(v3 < this.e.length) {
            Drawable v0_3 = this.e[v3];
            v1 = this.i[v3] * this.j / 255;
            if(v0_3 != null && v1 > 0) {
                ++this.k;
                v0_3.mutate().setAlpha(v1);
                --this.k;
                v0_3.draw(arg9);
            }

            ++v3;
        }

        if(!v2) {
            this.invalidateSelf();
        }
    }

    public final int getAlpha() {
        return this.j;
    }

    public final void invalidateSelf() {
        if(this.k == 0) {
            super.invalidateSelf();
        }
    }

    public final void setAlpha(int arg2) {
        if(this.j != arg2) {
            this.j = arg2;
            this.invalidateSelf();
        }
    }
}

